#!/bin/bash


#定义日志文件
log_file="/data/log/db_backup.log"
mkdir -p /data/log

ctime=$(date +%F_%H%M%S)

back_dir="/backup"
mkdir -p $back_dir

log_server="192.168.88.133"

/usr/bin/xtrabackup --backup --target-dir=$back_dir/full-backup-$ctime --user=root --password="@Zf123456"

status=$?
if [ $status -eq 0 ];then
    echo "$(date +%T) 备份成功" >> $log_file
else
    echo "$(date +%T) 备份失败" >> $log_file
    exit -1
fi

tar -czf $back_dir/full-backup-$ctime.tgz $back_dir/full-backup-${ctime}

status=$?
if [ $status -eq 0 ];then
    echo "$(date +%T) 备份成功" >> $log_file
else
    echo "$(date +%T) 备份失败" >> $log_file
    exit -2
fi

ssh root@${log_server} mkdir -p /db_backup2
scp ${back_dir}/full-backup-${ctime}.tgz root@${log_server}:/db_backup2

status=$?
if [ $status -eq 0 ];then
    echo "$(date +%T) 备份成功" >> $log_file
else
    echo "$(date +%T) 备份失败" >> $log_file
    exit -2
fi

find ${back_dir} -mtime +15 -type f -name "*.tgz" -exec rm -rf {} \;



